package org.example.Demo01;

import org.example.Join.JDBC_Join;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Scanner;

public class JDBC_Update {
    public JDBC_Update() {
    }
    private JDBC_Join jdbcJoin = new JDBC_Join();

    /*
    可以封装到一个类中
    * */
    public int update() throws Exception {
        //获取数据库连接
        Connection conn = jdbcJoin.join();
        //更新操作
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入更新后的brandName：");
        String brandName = sc.next();
        System.out.println("请输入更新后的companyName：");
        String companyName = sc.next();
        System.out.println("请输入更新后的ordered：");
        int ordered = sc.nextInt();
        System.out.println("请输入更新后的description：");
        String description = sc.next();
        System.out.println("请输入更新后的status：");
        int status = sc.nextInt();
        System.out.println("请输入更新后的id：");
        int id = sc.nextInt();

        String sql = "update tb_brand " +
                "set brand_name = ?,company_name = ?,ordered = ?,description = ?,status = ? where id = ?";
        PreparedStatement prestmt = conn.prepareStatement(sql);
        prestmt.setString(1,brandName);
        prestmt.setString(2,companyName);
        prestmt.setInt(3,ordered);
        prestmt.setString(4,description);
        prestmt.setInt(5,status);
        prestmt.setInt(6,id);

        int res = prestmt.executeUpdate();

        return res;
    }

}
